﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class usercontrol_ucMyAccount : System.Web.UI.UserControl
{
    TblUserAccountDAO userDAO = new TblUserAccountDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                Response.Redirect("~/LoginOrRegister.aspx");
                return;
            }
            else
            {
                LoadDetailUser();
            }
        }
    }
    private void LoadDetailUser()
    {
        try
        {
            TblUserAccount user = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
            txtEmail.Text = user.Email;
            txtFName.Text = user.FirstName;
            txtSName.Text = user.LastName;


            TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);
            TblShippTo shipto;
            shipto = shiptoDAO.GetAll(user.AccountID);
            if (shipto != null)
            {
                txtAddress.Text = shipto.Address;
                //cb_city.Text= shipto.City;
                // txtCountry.Text = shipto.Country;
                txtPhone.Text = shipto.Phone;

                cbcity.Items.Insert(0, new ListItem(shipto.City, " "));
                cbcity.SelectedIndex = -1;

                txtName.Text = shipto.Name;

            }
            else
            {

            }

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        TblUserAccount userAccount = null;
        try
        {

            // save Account                   

            userAccount = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];

            userAccount.AccountStatus = 1;
            userAccount.FirstName = txtFName.Text;
            userAccount.LastName = txtSName.Text;

            userDAO.Insert_Update(userAccount, 2);
            Session[Global.SESSION_LOGGED_USER] = userAccount;

            // save Shipping
            TblShippToDAO shippingDAO = new TblShippToDAO(Global.CONNECTION_STRING);
            TblShippTo shipto_update = new TblShippTo();

            shipto_update.AccountID = userAccount.AccountID;
            shipto_update.Name = txtName.Text.Trim();
            shipto_update.Address = txtAddress.Text.Trim();
            shipto_update.City = cbcity.SelectedItem.Text;
            shipto_update.Country = "VietNam";
            shipto_update.Phone = txtPhone.Text.Trim();
            shipto_update.Email = txtEmail.Text.Trim();
            shipto_update.ContactName = "";
            shipto_update.ShippingMethod = "";
            //shipto_update.ModifyDate1 = DateTime.Now;

            shippingDAO.Update(shipto_update);

            Session[Global.SESSION_SHIPTO] = shipto_update;

            bFlag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (bFlag)
            lblMessage.Text = "Cập nhật thành công";
    }   
}
